package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.ExceptionEntity;

/**
 * Created by qiuzhenhuan on 2019/3/25.
 */

public class ADA_Exception extends RecyclerView.Adapter<ADA_Exception.ViewHolder> {


    private Context mContext;

    private List<ExceptionEntity> mData;

    public ADA_Exception(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_exception_list, null);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtil.dip2px(mContext, 55));
        view.setLayoutParams(params);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        int titleBgColor = mContext.getResources().getColor(R.color.navigation_color);
        int normalBgColor = mContext.getResources().getColor(android.R.color.white);

        int titleTextColor = mContext.getResources().getColor(android.R.color.white);
        int normalTextColor = mContext.getResources().getColor(R.color.title_text_color);

        holder.tvType.setBackgroundColor(position == 0 ? titleBgColor : normalBgColor);
        holder.tvCount.setBackgroundColor(position == 0 ? titleBgColor : normalBgColor);
        holder.tvAmount.setBackgroundColor(position == 0 ? titleBgColor : normalBgColor);
        holder.tvAmountUpdate.setBackgroundColor(position == 0 ? titleBgColor : normalBgColor);

        holder.tvType.setTextColor(position == 0 ? titleTextColor : normalTextColor);
        holder.tvCount.setTextColor(position == 0 ? titleTextColor : normalTextColor);
        holder.tvAmount.setTextColor(position == 0 ? titleTextColor : normalTextColor);
        holder.tvAmountUpdate.setTextColor(position == 0 ? titleTextColor : normalTextColor);


        if (position == 0) {
            holder.tvType.setText("类别");
            holder.tvCount.setText("数量");
            holder.tvAmount.setText("金额");
            holder.tvAmountUpdate.setText("退货修改金额");
        } else {
            ExceptionEntity entity = mData.get(position - 1);
            holder.tvType.setText(StringUtils.get(entity.getType()));
            holder.tvCount.setText(String.format("%s个", StringUtils.getNumber(entity.getCount())));
            holder.tvAmount.setText(StringUtils.getFormattedMoneyWithUnit(entity.getAmount()));
            if (!StringUtils.isEmpty(entity.getReturnGainAmount()) && !StringUtils.isEmpty(entity.getReturnLoseAmount())) {
                holder.tvAmountUpdate.setText(String.format("%s（盈） | %s（亏）", StringUtils.getFormattedMoneyWithUnit(entity.getReturnGainAmount()),
                        StringUtils.getFormattedMoneyWithUnit(entity.getReturnLoseAmount())));
            } else {
                holder.tvAmountUpdate.setText("----");
            }
        }
        holder.viewLine.setVisibility(position != getItemCount() - 1 ? View.VISIBLE : View.GONE);

    }

    @Override
    public int getItemCount() {
        return ListUtil.isListEmpty(mData) ? 1 : mData.size() + 1;
    }

    public void setData(List<ExceptionEntity> mData) {
        this.mData = mData;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.view_line)
        View viewLine;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.tv_count)
        TextView tvCount;
        @BindView(R.id.tv_amount)
        TextView tvAmount;
        @BindView(R.id.tv_amount_update)
        TextView tvAmountUpdate;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
